package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserDao_lzl;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.User;
import com.itheima.service.UserService_lzl;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = UserService_lzl.class)
@Transactional
public class UserServiceImpl_lzl implements UserService_lzl {
    @Autowired
    private UserDao_lzl userDao_lzl;

    /*
    * 根据用户名查询user对象的所有角色及权限
    * */
    @Override
    public User findUser(String username) {
       return userDao_lzl.findUser(username);
    }
    /*
     * 分頁查詢
     * */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

            Page<Map> page = userDao_lzl.findPageByCondition(queryString);

//        List<User> users = page.getResult();
//        List<Object> list = new ArrayList<>();
//        Map<String, Object> map = new HashMap<>();
//        for (User user : users) {
//            if(user.getBirthday()!=null) {
//                Date birthday = user.getBirthday();
//
//            }
//        }

        return new PageResult(page.getTotal(),page.getResult());
    }

    /*
     * 添加用户及关联的角色
     * */
    @Override
    public void add(Integer[] roleIds, User user) throws Exception {
        String password1 = user.getPassword();
        String password = MD5Utils.md5(password1);
        user.setPassword(password);
        userDao_lzl.add(user);

        addCheckGroupIdAndCheckitemId(roleIds,user);
    }

    /*
     * 根据id，查询用户，并返回
     * */
    @Override
    public User findById(Integer id) {
        return userDao_lzl.findById(id);
    }

    /*
     * 将checkitemids集合和checkGroupId添加到中间表中
     * */
    public void addCheckGroupIdAndCheckitemId(Integer[] id,User user){
        if(id!=null&&id.length>0){
            for (Integer roleid : id) {
                Map<String, Integer> map = new HashMap<>();
                map.put("user_id",user.getId());
                map.put("role_id",roleid);
                userDao_lzl.addCheckGroupIdAndCheckitemId(map);
            }
        }
    }

    /*
     * 根据id，查询关联表，并返回roleIds
     * */
    @Override
    public List<Integer> findroleIdsByuserId(Integer id) {
        return  userDao_lzl.findroleIdsByuserId(id);
    }

    /*
     *
     * 1.根据CheckgroupId删除与之有关的关联表中的CheckItemId
     * 2.再修改CheckGroup
     * 3.后将CheckgroupId和checkitemid存入中间关联表
     * */
    @Override
    public void update(Integer[] id, User user) {
        Integer userId = user.getId();
        userDao_lzl.delete(userId);
        userDao_lzl.update(user);
        addCheckGroupIdAndCheckitemId(id,user);
    }
    /*
     * 根据id，删除
     * */
    @Override
    public void delete(int userid) throws RuntimeException {
        int count = userDao_lzl.findCountByuserId(userid);
        if(count>0){
            throw new RuntimeException();
        }else {
            userDao_lzl.deleteUser(userid);
        }
    }
    /*
     * 查询所有，进行回显到套餐管理
     * */
    @Override
    public List<User> findAll() {
        return userDao_lzl.findAll();
    }
}
